package com.example.test003.service.impl;

import com.example.test003.service.WeatherBaiKeService;
import com.example.test003.utils.ResponseResult;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Service
public class WeatherBaiKeServiceImpl implements WeatherBaiKeService {

    // 气象百科 -> 6个
    String qxbkPre = "src\\main\\resources\\qxbk\\";
    String qxzhPre = "src\\main\\resources\\qxzh\\";
    String suffix = ".txt";

    @Override
    public ResponseResult getQxyy(Integer id) {
        Path path = Paths.get(qxbkPre + "qxyy\\" + id + suffix);
        String msg = getMsg(path);
        return ResponseResult.okResult(msg);
    }

    @Override
    public ResponseResult get24Jq(Integer id) {
        Path path = Paths.get(qxbkPre + "24jq\\" + id + suffix);
        String msg = getMsg(path);
        return ResponseResult.okResult(msg);
    }

    @Override
    public ResponseResult getQxzs(Integer id) {
        Path path = Paths.get(qxbkPre + "qxzs\\" + id + suffix);
        String msg = getMsg(path);
        return ResponseResult.okResult(msg);
    }

    @Override
    public ResponseResult getQxsy(Integer id) {
        Path path = Paths.get(qxbkPre + "qxsy\\" + id + suffix);
        String msg = getMsg(path);
        return ResponseResult.okResult(msg);
    }

    @Override
    public ResponseResult getQxyb(Integer id) {
        Path path = Paths.get(qxbkPre + "qxyb\\" + id + suffix);
        String msg = getMsg(path);
        return ResponseResult.okResult(msg);
    }

    @Override
    public ResponseResult getQt(Integer id) {
        Path path = Paths.get(qxbkPre + "qt\\" + id + suffix);
        String msg = getMsg(path);
        return ResponseResult.okResult(msg);
    }

    // 气象灾害 -> 4个
    @Override
    public ResponseResult getQxyj(Integer id) {
        Path path = Paths.get(qxzhPre + "qxyj\\" + id + suffix);
        String msg = getMsg(path);
        return ResponseResult.okResult(msg);
    }

    @Override
    public ResponseResult getQhbh(Integer id) {
        Path path = Paths.get(qxzhPre + "qhbh\\" + id + suffix);
        String msg = getMsg(path);
        return ResponseResult.okResult(msg);
    }

    @Override
    public ResponseResult getYjbs(Integer id) {
        Path path = Paths.get(qxzhPre + "yjbs\\" + id + suffix);
        String msg = getMsg(path);
        return ResponseResult.okResult(msg);
    }

    @Override
    public ResponseResult getYjzs(Integer id) {
        Path path = Paths.get(qxzhPre + "yjzs\\" + id + suffix);
        String msg = getMsg(path);
        return ResponseResult.okResult(msg);
    }

    public String getMsg(Path path) {
        try {
            byte[] data = Files.readAllBytes(path);
            String result = new String(data, "utf-8");
            while (result == null) {
                System.out.println("读取文件内容出错");
            }
            System.out.println(result);
            return result;
        } catch (IOException e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
            return "读取文件内容出错";
        }
    }

}
